package com.lzmpt.imchat.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.lzmpt.imchat.R;

public class MenuPoPu extends PopupWindow implements View.OnClickListener {

    private View mPopView;
    private Activity context;
    private int mMenuType = 0;

    public MenuPoPu(Activity context,int mMenuType) {
        super(context);
        this.context = context;
        this.mMenuType = mMenuType;
        init(context);
        setPopupWindow();
    }

    private void init(Context context) {
        LayoutInflater inflater = LayoutInflater.from(context);
        if (mMenuType==1){
            mPopView = inflater.inflate(R.layout.popup_menu, null);
            mPopView.findViewById(R.id.tv_main_menu1).setOnClickListener(this);
            mPopView.findViewById(R.id.tv_main_menu2).setOnClickListener(this);
        }else {
            mPopView = inflater.inflate(R.layout.popup_menu_chat, null);
            mPopView.findViewById(R.id.tv_main_menu1).setOnClickListener(this);
            mPopView.findViewById(R.id.tv_main_menu2).setOnClickListener(this);
            mPopView.findViewById(R.id.tv_main_menu3).setOnClickListener(this);
            mPopView.findViewById(R.id.tv_main_menu4).setOnClickListener(this);
            mPopView.findViewById(R.id.tv_main_menu5).setOnClickListener(this);
        }


    }


    public void show() {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = 0.7f;
        context.getWindow().setAttributes(lp);
    }

    private void setPopupWindow() {
        this.setContentView(mPopView);// 设置View
        this.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);// 设置弹出窗口的宽
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);// 设置弹出窗口的高
        this.setFocusable(true);// 设置弹出窗口可
        this.setBackgroundDrawable(new ColorDrawable(0x00000000));// 设置背景透明
        this.setOutsideTouchable(true);
        this.setOnDismissListener(new OnDismissListener() { //退出popupwidon时显示父控件原来的颜色
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams lp = context.getWindow().getAttributes();
                lp.alpha = 1.0f;
                context.getWindow().setAttributes(lp);
            }
        });
    }

    private PopuItemClick mPopuItemClick;

    public void setPopuItemClick(PopuItemClick mPopuItemClick) {
        this.mPopuItemClick = mPopuItemClick;
    }

    @Override
    public void onClick(View v) {
        int type = 0;
        if (v.getId() == R.id.tv_main_menu1) {
            type = 1;
        } else if (v.getId() == R.id.tv_main_menu2) {
            type = 2;
        } else if (v.getId() == R.id.tv_main_menu3) {
            type = 3;
        } else if (v.getId() == R.id.tv_main_menu4) {
            type = 4;
        } else if (v.getId() == R.id.tv_main_menu5) {
            type = 5;
        }
        if (mPopuItemClick != null)
            mPopuItemClick.onItemOclick(v, type);
        dismiss();
    }


    public interface PopuItemClick {
        public void onItemOclick(View view, int type);
    }
}
